class Connection

  def self.connect(site)
    # let's say site.protocol returns a symbol name :xmlrpc, :blogger, etc
    connection = class_for(site.protocol)
    # return a connection
    connection.new(site)
  end

  # we will make this polymorphic later (reflection?)
  def self.class_for(protocol_name)
    if protocol_name == :xmlrpc
      XMLRPCConnection
    else
      # use proper exception
      raise "we only support xmlrpc"
    end
  end

end

class XMLRPCConnection < Connection
  
  def initialize(site)
    @site = site
    # create XML-RPC connection
    @server = XMLRPC::Client.new(@site.host, @site.path)
  end

  def publish(content)
    # publish the post
    @server.call('metaWeblog.newPost', @site.blog_id, @site.user, @site.pass, content, true)
  end

  def save_draft(content)
    # setting false the post won't be published
    @server.call('metaWeblog.newPost', @site.blog_id, @site.user, @site.pass, content, false)
  end

end
